package com.yellowpage.ca.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.UserRole;
import com.yellowpage.ca.service.UserRoleService;

public class UserRoleAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<UserRole> userRoleList = new ArrayList<UserRole>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private UserRole userRole;
	private UserRoleService userRoleService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_userRoleId;
		private String query_roleId;
		private String query_userId;
		private String query_createUser;
		private String query_createTime;
		
		private Map roleMap;
		private Map userMap;	
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.userRole = userRoleService.getUserRole(Long.parseLong(selectedId));
		}
		
		roleMap  = userRoleService.getRoleMap();
		userMap = userRoleService.getUserMap();
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("userRoleId", query_userRoleId);
	    	    queryMap.put("roleId", query_roleId);
	    	    queryMap.put("userId", query_userId);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("createTime", query_createTime);
	    	    	
		resultList = userRoleService.queryUserRoleByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
		
		userRole.setCreateTime(new Date());
		userRole.setCreateUser(loginUser.getUserId());	
	    
		userRoleService.saveUserRole(userRole);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    	    	    	    	    	    	    	    
		this.setValidatorResult("/ca2/UserRoleAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    userRole = userRoleService.getUserRole(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    userRoleService.updateUserRole(userRole);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    			    			    			    		this.setValidatorResult("/ca2/UserRoleUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.userRoleService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}
	
	public UserRole getUserRole() {
		return userRole;
	}
	 
	 
	 public void setUserRoleService(UserRoleService userRoleService) {
		this.userRoleService = userRoleService;
	}
	
	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}

	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_userRoleId() {
        return this.query_userRoleId;
    }
    
    public void setQuery_userRoleId(String userRoleId){
        this.query_userRoleId = userRoleId;
    }    

        public String getQuery_roleId() {
        return this.query_roleId;
    }
    
    public void setQuery_roleId(String roleId){
        this.query_roleId = roleId;
    }    

        public String getQuery_userId() {
        return this.query_userId;
    }
    
    public void setQuery_userId(String userId){
        this.query_userId = userId;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }

	public Map getRoleMap() {
		return roleMap;
	}

	public void setRoleMap(Map roleMap) {
		this.roleMap = roleMap;
	}

	public Map getUserMap() {
		return userMap;
	}

	public void setUserMap(Map userMap) {
		this.userMap = userMap;
	}    

    
	
}
